package com.woniuxy.rabbitmqspringboot.plugin;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author zhangyong
 * @Date 2021/8/12 0012下午 4:25
 */
@Component
public class PluginProductor {
    @Resource
    private AmqpTemplate amqpTemplate;
    //参数2：当前消息的延迟时间（TTL生存时间）
    public void send(String data ,int delayTime){
        //创建消息处理器
        MessagePostProcessor processor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置ttl时间
                message.getMessageProperties().setDelay(delayTime);//毫秒
                return message;
            }
        };

        System.out.println("发送时间"+new Date());
        //发送消息并指定消息的TTL时间
        amqpTemplate.convertAndSend("plugin_delay_exchange","plugin_delay",data,processor);
    }
}
